package com.erp.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.erp.po.Customers;
import com.erp.po.Employees;
import com.erp.po.OrderDetail;
import com.erp.po.Orders;
import com.erp.po.Products;
import com.erp.service.CustomersServiceImpl;
import com.erp.service.EmployeesServiceImpl;
import com.erp.service.OrderDetailsServiceImpl;
import com.erp.service.OrderServiceImpl;
import com.google.gson.Gson;

@WebServlet("/admin/orders/OrderDetailServlet")
public class OrderDetailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private OrderServiceImpl os;
	private EmployeesServiceImpl es;
	private CustomersServiceImpl cs;
	private OrderDetailsServiceImpl adsi;

	public OrderDetailServlet() {
		os = new OrderServiceImpl();
		es = new EmployeesServiceImpl();
		cs = new CustomersServiceImpl();
		adsi = new OrderDetailsServiceImpl();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String op = request.getParameter("op");
		
		System.out.println("信息 : "+op+"  ---- OrderDetailServlet");
		
		if("showDetailByOrderID".equals(op)) {
			// 根据orderID 查询 对应的订单详情
			showDetailByOrderID(request,response);
			
		} else if("readyEditDetail".equals(op)) {
			// 准备修改订单详情
			readyEditDetail(request,response);
			
		} else if("doEditOrderDetail".equals(op)) {
			// 修改订单-详情
			doEditOrderDetail(request,response);
			
		} else if("delDetailByDetailId".equals(op)) {
			// 根据详情id 删除详情记录
			delDetailByDetailId(request,response);
			
		}
		
		
	}

	/**
	 * 根据详情id 删除详情记录
	 */
	protected void delDetailByDetailId(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		int detailId = Integer.parseInt(request.getParameter("detailId"));
		String orderID = request.getParameter("orderId");
		
		// System.out.println(orderID);
		int i = adsi.removeDetailById(detailId);
		
		if(i > 0) {
			// 根据 orderID 查询detail数据的条数
			int count = adsi.findDetailCountByOrderID(orderID);
			// System.out.println("信息 : "+count);
			if(count > 0 ) {
				response.getWriter().print("true");
			} else if(count == 0) {
				response.getWriter().print("removeOrder");
			}
		} else {
			response.getWriter().print("false");
		}
		
	}

	/**
	 * 修改订单-详情
	 */
	protected void doEditOrderDetail(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		String[] id = request.getParameterValues("detailID");
		String orderID = request.getParameter("orderId");
		String orderDate = request.getParameter("orderTime");
		int customerID = Integer.parseInt(request.getParameter("customers"));
		int empID = Integer.parseInt(request.getParameter("employees"));
		String[] proArr = request.getParameterValues("products");
		String[] quaArr = request.getParameterValues("quantity");
		String[] disArr = request.getParameterValues("discount");
		
		// System.out.println(orderID);
		// System.out.println(proArr.length);
		
		// 修改订单-详情之前,判断是否有增加数据[多出的数据进行添加]
		// int count = adsi.findDetailCountByOrderID(orderID);
		
		// 使用循环-判断在 orderId 下  是否有 该productId的记录
		ArrayList<OrderDetail> modifyList = new ArrayList<>();
		ArrayList<OrderDetail> addList = new ArrayList<>();

		for (int j = 0; j < id.length; j++) {
			if (Integer.parseInt(id[j]) != 0) {
				// 修改
				modifyList.add(new OrderDetail(Integer.parseInt(id[j]), Integer.parseInt(quaArr[j]),
						Double.parseDouble(disArr[j]), new Orders(orderID), new Products(Integer.parseInt(proArr[j]))));

			} else if (Integer.parseInt(id[j]) == 0) {
				// 增加
				addList.add(new OrderDetail(Integer.parseInt(quaArr[j]), Double.parseDouble(disArr[j]),
						new Orders(orderID), new Products(Integer.parseInt(proArr[j]))));
			}
		}
		
		boolean modifyResult = false;
		boolean AddResult = false;
		
		if(!(modifyList.isEmpty())) {
			// 修改
			OrderDetail[] modifyArr = new OrderDetail[modifyList.toArray().length];
			for (int i = 0; i < modifyList.toArray().length; i++) {
				modifyArr[i] = modifyList.get(i);
			}
			modifyResult = adsi.modifyOrderDetails(new Orders(orderID, orderDate, new Customers(customerID), new Employees(empID)), modifyArr);
		} else {
			modifyResult = true;
		}
		
	
		if(!(addList.isEmpty())) {
			OrderDetail[] addArr = new OrderDetail[addList.toArray().length];
			for (int j = 0; j < addList.toArray().length; j++) {
				addArr[j] = addList.get(j);
			}
			
			AddResult = adsi.addDetailsByOrderId(addArr);
		} else {
			AddResult = true;
		}
		
		System.out.println(modifyResult+"   "+AddResult);
		
		if(modifyResult && AddResult) {
			response.getWriter().print("true");
		} else {
			response.getWriter().print("false");
		}
		
	}

	/**
	 * 准备修改订单详情
	 */
	protected void readyEditDetail(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		// 所有员工数据
		List<Employees> empList = es.findAllEmps();
		// 所有客户数据
		List<Customers> custList = cs.findAllCusts();
		// 所有商品数
		List<Products> prodList = os.findProducts();
		
		Object[] arr = {empList,custList,prodList};
		
		// 将数据转换为json格式的字符串
		Gson gson = new Gson();
		response.getWriter().print(gson.toJson(arr));
	}

	/**
	 * 根据orderID 查询 对应的订单详情[准备进行修改]
	 */
	protected void showDetailByOrderID(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		String orderID = request.getParameter("orderID");
		
		// System.out.println(orderID);
		
		// 查询出来的订单详情数据
		List<OrderDetail> DetailList = adsi.findOrderDetailByOrderID(orderID);
		
/*//		// 所有员工数据
		List<Employees> empList = es.findAllEmps();
//		// 所有客户数据
		List<Customers> custList = cs.findAllCusts();
//		// 所有商品数
		List<Products> prodList = os.findProducts();*/
//		
		/*System.out.println(list.size());
		for (OrderDetail o : list) {
			System.out.println(o);
		}*/
		
		// Object[] arr = {DetailList,empList,custList,prodList};
		
		// 将数据转换为json格式的字符串
		Gson gson = new Gson();
		response.getWriter().print(gson.toJson(DetailList));
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
